package com.lzh.sort;

/**
 * @author liuzhihao
 * @create 2022-03-31 0:40
 */
public class demo2 {

    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        node1.setNext(node2);
        node2.setNext(node3);

        ReverseList(node1);
    }

    public static ListNode ReverseList(ListNode head) {
        ListNode newHead = new ListNode(0);
        if (head == null) {
            return null;
        }
        ListNode temp = head;
        ListNode nodeTemp = head;
        while (true){
            if (nodeTemp == null) {
                break;
            }
            nodeTemp = nodeTemp.next;
            temp.next = newHead.next;
            newHead.next = temp;
            temp = nodeTemp;
            nodeTemp = nodeTemp;
        }

        return newHead.next;

    }

    public static class ListNode {

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public ListNode getNext() {
            return next;
        }

        public void setNext(ListNode next) {
            this.next = next;
        }

        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
}

